package com.situ.day4;

import org.junit.Test;

public class ArrayDemo {
    @Test
    public void test1(){
        int[] array = new int[4];
        //数组访问，数组名[下标]
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;
        System.out.println(array.length);//4
        //for (int i =  0; i <= array.length - 1;i++){
        //   System.out.println(i);
        for (int i =  0; i <= array.length - 1;i++){
            System.out.println("i:" + i);
            System.out.println(array[i]);
        }
    }

    //定义数组
    @Test
    public void test2(){
        int[] array = new int[4];
        //数组访问，数组名[下标]
        array[0] = 23;
        array[1] = 3;
        array[2] = 4;
        array[3] = 24;

        //在定义时直接赋值
        int[] array1 = {23,45,7,9,67};
        System.out.println(array1.length);
        int[] array2 =new int[] {23,45,7,9,67,677};
        System.out.println(array2.length);
    }

    @Test
    public void test3(){
        int[] array = {23,45,7,9};
        for (int i = 0; i <= array.length;i++){
            System.out.println(array[i]);
        }
    }

    @Test
    public void test114() {
        int[] array = {23, 45, 67, 2, 12};
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        System.out.println("sum: " + sum);
    }

    @Test
    public void test111() {
        int[] array = {23, 45, 67, 2, 12};
        //array.fori
        //初始认为0这个位置是最大的
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        System.out.println("max: " + max);
    }

    @Test
    public void test112() {
        int[] array = {23, 56, 87, 2, 12};
        //方法调用：方法名(参数)
        int max = getMax(array);
        System.out.println("max: " + max);
    }

    @Test
    public void test153() {
        int[] array = {45, 67, 92, 12};
        //方法调用：方法名(参数)
        int max = getMax(array);
        System.out.println("max: " + max);
    }

    /**
     * 求最大值
     * @param array 要求最大值的数组
     * @return 返回数组最大值
     */
    public int getMax(int[] array) {
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }

    @Test
    public void test4(){
        int[] array = {23,45,67,2,12};
        //array.fori
        //初始认为0这个结果是最大的
        int max = array[0];
        for (int i = 0;i < array.length;i++){
            if (array[i] >max){
                max = array[i];
            }
        }
        System.out.println("max:"+ max);
    }




    @Test
    public void test5(){
        int[] array = {23,45,67,2,12};
        int min = array[0];
        for (int i = 0;i < array.length;i++){
            if (array[i] < min){
                min = array[i];
            }
        }
        System.out.println("min:"+ min);
    }

    @Test
    public void test8(){
        int[] array = {40,17,21,1};
        for (int i =1; i <= array.length -1; i++){
            for (int j =0; j < array.length - i;j++){
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
        for (int i = 0;i < array.length;i++) {
            System.out.println(array[i]);
        }
    }


}
